package carltechnology.com.mahjonggame.presenter.impl;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import carltechnology.com.mahjonggame.R;
import carltechnology.com.mahjonggame.base.MyApplication;
import carltechnology.com.mahjonggame.mode.IData;
import carltechnology.com.mahjonggame.mode.event.FragmentViewEvent;
import carltechnology.com.mahjonggame.mode.impl.MahjongGameMode;
import carltechnology.com.mahjonggame.presenter.IMajhongGamePresenter;
import carltechnology.com.mahjonggame.utils.Utils;
import carltechnology.com.mahjonggame.utils.dao.impl.MahjongDatabaseManager;
import carltechnology.com.mahjonggame.views.IMahjongGameView;
import rx.Subscriber;
import rx.Subscription;

/**
 * Created by WZTCM on 2016/6/1.
 */
public class MajhongGamePresenter implements IMajhongGamePresenter,AdapterView.OnItemClickListener{
    private static MajhongGamePresenter instance = null;
    private IMahjongGameView iMahjongGameView;
    private List<IData> list ;
    private final MahjongGameAdapter adapter;
   // private Subscriber<IData> subs;
    private Subscription subscription;
    private MajhongGamePresenter(){
        list = new CopyOnWriteArrayList<>();
        adapter = new MahjongGameAdapter();

    }
    private Subscriber<IData> getSubs(){
        Subscriber<IData> subscriber= new Subscriber<IData>() {
            @Override
            public void onCompleted() {
                Log.d("MajhongGamePresenter","info finished");
            }

            @Override
            public void onError(Throwable e) {
                Log.e("MajhongGamePresenter",e.toString());
            }

            @Override
            public void onNext(IData iData) {
                // System.out.println(Thread.currentThread());
                list.add(iData);
                adapter.notifyDataSetChanged();
                //iMahjongGameView.getHandler().sendMessage(new Message());
            }
        };
        return subscriber;
    }
    @Override
    public void getMahjongGameAllInfo() {
     //   subs = getSubs();
        subscription=MahjongDatabaseManager.getInstance().getMahjongGameInfo(getSubs());
    }

    @Override
    public void updateMahjongGameInfo() {
        adapter.notifyDataSetChanged();
    }

    @Override
    public void setAttachView(final IMahjongGameView view) {
        iMahjongGameView = view;
        iMahjongGameView.getRecyvleView().setLayoutManager(new LinearLayoutManager(iMahjongGameView.getContext()));
        iMahjongGameView.getRecyvleView().setAdapter(adapter);
    }

    @Override
    public void endFragment() {
        subscription.unsubscribe();
        list.clear();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        System.out.println("clicked");
        IData iData=list.get(position);
        MahjongGameMode mode = MahjongGameMode._of(iData.getId(),iData.getSimpleName(),iData.getDate());
        MyApplication.getDataPool().setMahjongGameMode(mode);
        Utils.getEventBus().post(new FragmentViewEvent(FragmentViewEvent.EVENT.EVENT_MAHJONG_DETAIL_FRAGMENT));
    }


    class MahjongGameAdapter extends RecyclerView.Adapter<MahjongGameAdapter.MyViewHolder>{


        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater
            .from(iMahjongGameView.getContext()).inflate(R.layout.mahjonggame_content_item,parent,false));
            return myViewHolder;
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            IData data = list.get(position);
            String [] names = data.getName();
            holder.getMyName1().setText(names[0]);
            holder.getMyName2().setText(names[1]);
            holder.getMyName3().setText(names[2]);
            holder.getMyName4().setText(names[3]);
            holder.getMyDate().setText(data.getDate());

        }

        class MyViewHolder extends RecyclerView.ViewHolder{
            @InjectView(R.id.mahjonggame_item_date)
            TextView myDate;
            @InjectView(R.id.mahjonggame_item_name1)
            TextView myName1;
            @InjectView(R.id.mahjonggame_item_name2)
            TextView myName2;
            @InjectView(R.id.mahjonggame_item_name3)
            TextView myName3;
            @InjectView(R.id.mahjonggame_item_name4)
            TextView myName4;
            public MyViewHolder(View itemView) {
                super(itemView);
                ButterKnife.inject(this,itemView);
            }

            public TextView getMyName1() {
                return myName1;
            }

            public TextView getMyName2() {
                return myName2;
            }

            public TextView getMyName3() {
                return myName3;
            }

            public TextView getMyName4() {
                return myName4;
            }

            public TextView getMyDate() {
                return myDate;
            }
        }
    }
    public static MajhongGamePresenter getInstance(IMahjongGameView iMahjongGameView){
        if (instance==null)
        {
            instance = new MajhongGamePresenter();
        }
        if (!instance.list.isEmpty()){
            instance.list.clear();
        }
        instance.getMahjongGameAllInfo();
        instance.setAttachView(iMahjongGameView);
        return instance;
    }

}
